package com.huabo.contract.mysql.mapper;

import com.hbfk.entity.TblAttachment;
import com.hbfk.util.PageInfo;
import com.huabo.contract.mysql.entity.TblContractPlannodeMySql;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author huabo
 * @since 2022-03-14
 */
public interface TblContractPlannodeMySqlMapper extends BaseMapper<TblContractPlannodeMySql> {




    @SelectProvider(type=TblContractPlannodeMySqlMapperSqlConfig.class,method="findPlanNodeListForCollection")
    List<TblContractPlannodeMySql> findPlanNodeListForCollection(PageInfo<TblContractPlannodeMySql> pageInfo, TblContractPlannodeMySql node);

    @SelectProvider(type=TblContractPlannodeMySqlMapperSqlConfig.class,method="selectCountByPageInfo")
    Integer selectCountByPageInfo(TblContractPlannodeMySql node);

    @Select("SELECT TCP.NODEID,TCP.NODECONTENT,TCP.PROJECTID,TCP.PLANSTARTDATE,TCP.PLANENDDATE,TCP.NODEPOST,TCP.NODEPLANPAYDATE,TCP.DISPATCHSTAFF as jbstaffid,TS.REALNAME,TCP.DISPATCHDEPT as jbunitid,ORG.ORGNAME,TCP.NODEMONEY,PLANNODESTATUS FROM TBL_CONTRACT_PLANNODE TCP LEFT JOIN TBL_STAFF TS ON TCP.DISPATCHSTAFF = TS.STAFFID LEFT JOIN TBL_ORGANIZATION ORG ON TCP.DISPATCHDEPT = ORG.ORGID WHERE TCP.PROJECTID = #{contractid}  ORDER BY TCP.NODEID ")
    List<TblContractPlannodeMySql> findPlanNodeListByContractId(BigDecimal contractid);

    @SelectProvider(type = TblContractPlannodeMySqlMapperSqlConfig.class,method = "findPlanNodeListForPayment")
    List<TblContractPlannodeMySql> findPlanNodeListForPayment(PageInfo<TblContractPlannodeMySql> pageInfo, TblContractPlannodeMySql node);

    @SelectProvider(type = TblContractPlannodeMySqlMapperSqlConfig.class,method = "findPlanNodeListForPaymentCount")
    Integer findPlanNodeListForPaymentCount(TblContractPlannodeMySql node);

//    @Select("SELECT SUM(NODEMONEY) FROM TBL_CONTRACT_PLANNODE WHERE PROJECTID = #{contractId}")
//    BigDecimal findeSumMoneyByContractId(Integer contractId);

    @InsertProvider(type = TblContractPlannodeMySqlMapperSqlConfig.class,method = "saveContractPlannode")
    @Options(useGeneratedKeys=true, keyProperty="nodeid", keyColumn="NODEID")
    void saveContractPlannode(TblContractPlannodeMySql node);

    @SelectProvider(type = TblContractPlannodeMySqlMapperSqlConfig.class,method = "findeSumMoneyByContractId")
    BigDecimal findeSumMoneyByContractId(Integer contractId, BigDecimal nodeid);

    @UpdateProvider(type = TblContractPlannodeMySqlMapperSqlConfig.class,method = "updateContractPlannode")
    void updateContractPlannode(TblContractPlannodeMySql node);

    @Delete("DELETE FROM TBL_CONTRACT_PLANNODE WHERE NODEID = #{nodeId}")
    void removeContractPlannode(Integer nodeId);

    @Select("SELECT COUNT(NODEID) FROM TBL_CONTRACT_PLANNODE WHERE PROJECTID = #{contractId}")
    Integer countPlanByContractId(BigDecimal contractId);

    @Select("SELECT SUM(NODEPOST) FROM TBL_CONTRACT_PLANNODE WHERE PROJECTID = #{contractId}")
    Integer sumPlanPostPoint(BigDecimal contractId);

    @SelectProvider(type = TblContractPlannodeMySqlMapperSqlConfig.class,method = "findContractNodeListByPageInfo")
    List<TblContractPlannodeMySql> findContractNodeListByPageInfo(PageInfo<TblContractPlannodeMySql> pageInfo);

    @SelectProvider(type = TblContractPlannodeMySqlMapperSqlConfig.class,method = "findCountContractNodeByPageInfo")
    Integer findCountContractNodeByPageInfo(PageInfo<TblContractPlannodeMySql> pageInfo);

    @Update("UPDATE TBL_CONTRACT_PLANNODE SET PLANNODESTATUS = #{planStatus},feedback= #{feedback} WHERE NODEID = #{nodeId}")
    void modifyPlanNodeStatus(Integer nodeId, Integer planStatus, String feedback);

    @Select("SELECT TCP.NODEID,TCP.NODECONTENT,TCP.PROJECTID,TCP.PLANSTARTDATE,TCP.PLANENDDATE,TCP.NODEPOST,TCP.NODEPLANPAYDATE,TCP.DISPATCHSTAFF,TS.REALNAME,TCP.DISPATCHDEPT,ORG.ORGNAME,TCP.nodemoney FROM TBL_CONTRACT_PLANNODE TCP LEFT JOIN TBL_STAFF TS ON TCP.DISPATCHSTAFF = TS.STAFFID LEFT JOIN TBL_ORGANIZATION ORG ON TCP.DISPATCHDEPT = ORG.ORGID WHERE TCP.NODEID = #{nodeId}")
    TblContractPlannodeMySql findBynodeId(Integer nodeId);

    @Select("SELECT * FROM TBL_CONTRACT_PLANNODE WHERE PROJECTID = #{contractId}")
    List<TblContractPlannodeMySql> getListPlannode(String contractId);

    @Select("SELECT TCP.NODEID,TCP.NODECONTENT,TCP.PROJECTID,TCP.PLANSTARTDATE,TCP.PLANENDDATE,TCP.NODEPOST,TCP.NODEPLANPAYDATE,TCP.DISPATCHSTAFF,TS.REALNAME,TCP.DISPATCHDEPT,ORG.ORGNAME,TCP.nodemoney FROM TBL_CONTRACT_PLANNODE TCP LEFT JOIN TBL_STAFF TS ON TCP.DISPATCHSTAFF = TS.STAFFID LEFT JOIN TBL_ORGANIZATION ORG ON TCP.DISPATCHDEPT = ORG.ORGID WHERE TCP.NODEID = #{nodeId}")
    TblContractPlannodeMySql selectByPlanId(Integer nodeId);

    @Select("SELECT TCP.NODEID,TCP.NODECONTENT,TCP.PROJECTID ,TCP.PLANSTARTDATE ,TCP.PLANENDDATE ,TCP.NODEPOST ,TCS.SPNODEID AS TCPSPNODEID,TCS.STARTDATE AS TCSSTARTDATE,TCS.ENDDATE AS TCSENDDATE,TCS.NODEMEMO AS TCSNODEMEMO,TCS.NODECONTENT AS TCSNODECONTENT,TCS.NODEPOST AS TCSNODEPOST,TCS.NODEMONEY AS TCSNODEMONEY,TCS.NODEFINISHDATE AS TCSNODEFINISHDATE,TCU.DCTYPE AS TCUDCTYPE,TCP.NODEPLANPAYDATE,TCS.ISWY FROM TBL_CONTRACT_PLANNODE TCP LEFT JOIN TBL_CYHW_UNIT TCU ON TCP.PROJECTID = TCU.CONTRACTID LEFT JOIN TBL_CONTRACT_SPNODE TCS ON TCP.NODEID = TCS.NODEID WHERE TCP.NODEID = #{planId}")
    TblContractPlannodeMySql findWriteContractPlanNode(Integer planId);

    @Select("SELECT ATTID,ATTNAME,ATTPATH,ATTSIZE from TBL_ATTACHMENT WHERE ATTID IN (SELECT ATTID FROM TBL_CONTRACTNODE_ATT WHERE ATTTYPE = 1 AND NODEID = #{planId})")
    List<TblAttachment> findeWriteContractPlanFileInfo(Integer planId);

    @Select("SELECT * FROM TBL_CONTRACT_PLANNODE WHERE NODEID = #{nodeId}")
    TblContractPlannodeMySql findPlannodeById(BigDecimal nodeid);
}
